package com.hx.hc.nio;

import java.nio.IntBuffer;

/**
 * @author jxlgcmh
 * @date 2020-05-21 00:05
 * @description  Buffer类的常用方法
 */
public class NioBuffer {
    public static void main(String[] args) {
        IntBuffer intBuffer =IntBuffer.allocate(20);

        // 基本属性
        System.out.println("position="+intBuffer.position());
        System.out.println("limit="+intBuffer.limit());
        System.out.println("capacity="+intBuffer.capacity());

        // put 方法
        for (int i = 0; i < 5; i++) {
            intBuffer.put(i);
        }
        System.out.println("=======分割线=======");

        System.out.println("position="+intBuffer.position());
        System.out.println("limit="+intBuffer.limit());
        System.out.println("capacity="+intBuffer.capacity());

        System.out.println("=======分割线=======");
        // 翻转缓冲区
        intBuffer.flip();
        System.out.println("position="+intBuffer.position());
        System.out.println("limit="+intBuffer.limit());
        System.out.println("capacity="+intBuffer.capacity());

        System.out.println("=======分割线=======");
        // 从缓存去读取数据  读到2的位置
        for (int i = 0; i < 2; i++) {
            int tmp = intBuffer.get();
            System.out.println(tmp);
        }
        System.out.println("position="+intBuffer.position());
        System.out.println("limit="+intBuffer.limit());
        System.out.println("capacity="+intBuffer.capacity());

        System.out.println("=======分割线=======");
        // 读取到3的位置
        for (int i = 0; i < 3; i++) {
            int tmp = intBuffer.get();
            System.out.println(tmp);
        }
        System.out.println("position="+intBuffer.position());
        System.out.println("limit="+intBuffer.limit());
        System.out.println("capacity="+intBuffer.capacity());

        System.out.println("=======分割线=======");
        //倒带方法的使用
        intBuffer.rewind();
        System.out.println("position="+intBuffer.position());
        System.out.println("limit="+intBuffer.limit());
        System.out.println("capacity="+intBuffer.capacity());

        System.out.println("=======分割线=======");
        // 调用mark方法
        for (int i = 0; i < 5; i++) {
            if (i==2) {
                // 标记了在第三个位置
                intBuffer.mark();
            }
            int tmp2 = intBuffer.get();
            System.out.println(tmp2);
        }
        System.out.println("position="+intBuffer.position());
        System.out.println("limit="+intBuffer.limit());
        System.out.println("capacity="+intBuffer.capacity());

        System.out.println("=======分割线=======");
        // rest 方法的使用
        System.out.println("position="+intBuffer.position());
        System.out.println("limit="+intBuffer.limit());
        System.out.println("capacity="+intBuffer.capacity());
        intBuffer.reset();
        for (int i = 2; i < 5; i++) {
            int tmp3 = intBuffer.get();
            System.out.println(tmp3);
        }
        System.out.println("=======分割线=======");
        System.out.println("position="+intBuffer.position());
        System.out.println("limit="+intBuffer.limit());
        System.out.println("capacity="+intBuffer.capacity());

        // clear 方法的使用
        intBuffer.clear();
        System.out.println("=======分割线=======");
        System.out.println("position="+intBuffer.position());
        System.out.println("limit="+intBuffer.limit());
        System.out.println("capacity="+intBuffer.capacity());

    }

}
